package GUI;
import Log.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;

import client.ClientConsole;
//import client.ClientMain;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Button;
import java.awt.Insets;
import java.util.ArrayList;
import java.awt.Dialog.ModalExclusionType;

public class MainFrame extends JFrame implements ActionListener,Runnable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel innerPanel;
	JButton btnViewLog;
	private ConsoleGUI CG;
	JButton btnShowDB;
	JButton btnInsertToDb;
	String[] inputData;
	public static MainFrame frame;
	static ClientSideGUI GetDataFromGUI;
	private ClientConsole clientFrame; // refrence to the connection status
	DialogInsertDB dbInsert;
	public JButton btnTryBtn;
	/**
	 * Launch the application.
	 */
	
	public static void main(String[] args) {
		//new Thread(new MainFrame()).start();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					 GetDataFromGUI = new ClientSideGUI();
					 frame = new MainFrame(GetDataFromGUI);
					frame.setVisible(true);
				
					//frame.wait();
					
				} catch (Exception e) {
				//	e.printStackTrace();
				}
			
			
			}
		});
		
		
	}

	/**
	 * Create the frame.
	 */
	public MainFrame(){
		BuildGUI();
		Log.getInstance();
		Log.CloseLogWindow();
	}
	
	public MainFrame(JPanel defaultScreen) {
		BuildGUI();
		getContentPane().add(defaultScreen);
		getContentPane().add(defaultScreen,BorderLayout.CENTER);
		Log.getInstance();
		Log.CloseLogWindow();
	}
	
	
	private void BuildGUI(){
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 683, 434);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
	
		//Image  image = new Image("Image/icm.jpg");
	//	ImageIcon icon = createImageIcon("icm.jpg‬",
        //        "G16 Logo");
		JLabel label = new JLabel("Main Frame");
		label.setForeground(Color.DARK_GRAY);
		label.setFont(new Font("Tahoma", Font.PLAIN, 40));
		
		getContentPane().add(label, BorderLayout.NORTH);
		
		
	//	getContentPane().add(image, BorderLayout.NORTH);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.EAST);
		
		// getContentPane().add(Console, BorderLayout.EAST);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{89, 0};
		gbl_panel.rowHeights = new int[]{23, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		 btnViewLog = new JButton("View Log");
		GridBagConstraints gbc_btnViewLog = new GridBagConstraints();
		gbc_btnViewLog.insets = new Insets(0, 0, 5, 0);
		gbc_btnViewLog.gridx = 0;
		gbc_btnViewLog.gridy = 0;
		panel.add(btnViewLog, gbc_btnViewLog);
		
		 btnShowDB = new JButton("Show DB");
		
		GridBagConstraints gbc_btnShowDB = new GridBagConstraints();
		gbc_btnShowDB.insets = new Insets(0, 0, 5, 0);
		gbc_btnShowDB.gridx = 0;
		gbc_btnShowDB.gridy = 1;
		panel.add(btnShowDB, gbc_btnShowDB);
		
		 btnInsertToDb = new JButton("Insert to DB");
		GridBagConstraints gbc_btnInsertToDb = new GridBagConstraints();
		gbc_btnInsertToDb.insets = new Insets(0, 0, 5, 0);
		gbc_btnInsertToDb.gridx = 0;
		gbc_btnInsertToDb.gridy = 2;
		panel.add(btnInsertToDb, gbc_btnInsertToDb);
		
		btnTryBtn = new JButton("Try Btn");
		GridBagConstraints gbc_btnTryBtn = new GridBagConstraints();
		gbc_btnTryBtn.gridx = 0;
		gbc_btnTryBtn.gridy = 3;
		panel.add(btnTryBtn, gbc_btnTryBtn);
		//btnViewLog.addActionListener(this);
		//btnShowDB.addActionListener(this);
		btnShowDB.setVisible(false);
		//btnInsertToDb.addActionListener(this);
		btnInsertToDb.setVisible(false);
		

	}

	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == btnViewLog)
			Log.ShowLogWindow();
		else if (event.getSource() == btnShowDB){
			clientFrame.SendMsgToServer("BringDB");
		
		
			
		}
		else if (event.getSource() == btnInsertToDb){
			 dbInsert = new DialogInsertDB();
			
		}
	}
	
	
	public void addCompponenetToMainFrame(Component comp){
		this.removeAll();
		getContentPane().add(comp,BorderLayout.CENTER);
		
		//getContentPane().setVisible(true);
	}
	public void Switch2Console(){
		 CG = new ConsoleGUI ();
		 this.getContentPane().add(CG,BorderLayout.CENTER);
		 frame.setVisible(true);
		 btnShowDB.setVisible(true);
		 btnInsertToDb.setVisible(true);
	}
	
	public void setClientRef(ClientConsole clientFrame){
		this.clientFrame = clientFrame;
	}
	public ClientConsole getClientRef(){
		return clientFrame;
	}
	public ConsoleGUI getConsoleRef(){
		return CG;
	}
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected ImageIcon createImageIcon(String path,
	                                           String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	       Log.addError("Couldn't find file: " + path);
	        return null;
	    }
	}

	public void setStringsToDB() {
		// TODO Auto-generated method stub
		ArrayList<String> al = new ArrayList<String>();
		inputData  = dbInsert.getAllFields();
		//inputData[3] = "InsertToDB";
		for (int i=0;i<=2;i++)
			al.add(inputData[i]);
		
		clientFrame.SendMsgToServer(al);
		
	}
	public void run(){
		 GetDataFromGUI = new ClientSideGUI();
		 frame = new MainFrame(GetDataFromGUI);
		frame.setVisible(true);
		//Log.addError("Started Thread GUI");
		while(true){
		//	Log.addError("a live");
	
		}
	}

	public void setClientRef(String IP, int PORT) {
		// TODO Auto-generated method stub
		clientFrame= new ClientConsole(IP,PORT);
		
	}
	
	public void ServerDownMSG(){
		
		JOptionPane.showMessageDialog(frame, "Server is down!","Server Error!", JOptionPane.ERROR_MESSAGE, null);
	}
	public void SetScrolltoMax(){
		CG.getVerticalScrollBar().setValue( 	CG.getVerticalScrollBar().getMaximum() );
		
	}
}
